package com.gem.growingtree.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.gem.growingtree.model.PageBean3;
import com.gem.growingtree.model.ShowClasses;
import com.gem.growingtree.service.ClassAccountService;

@Controller
public class ClassAccountController {
	@RequestMapping("/classAccount")
	public ModelAndView ShowClassAccount(HttpServletRequest request, HttpServletResponse response){
		int pageCode = 0;
		String pc = request.getParameter("pc");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		ClassAccountService pas = new ClassAccountService();
		try {
			PageBean3 pb = pas.findByPage(pageCode, pageSize);
			request.setAttribute("pageBean3", pb);
			return new ModelAndView("/classes");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	// 查找某个班级信息
	@RequestMapping("/showCertainClass")
	public ModelAndView ShowClassAccount2(HttpServletRequest request, HttpServletResponse response,String showC){
		int pageCode = 0;
		String pc = request.getParameter("pc");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		ClassAccountService pas = new ClassAccountService();
		try {
			PageBean3 pb = pas.findByPage2(pageCode, pageSize,showC);
			request.setAttribute("pageBean3", pb);
			return new ModelAndView("/classes");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	// 修改某个班级信息
	@RequestMapping("/modifyClasses")
	public String modifyClasses(HttpServletRequest request,
			HttpServletResponse response, ShowClasses showClasses) {
		String classId = request.getParameter("classId");
		if (classId != null) {
			try {
				ClassAccountService classAccountService = new ClassAccountService();
				classAccountService.updateClasses(Integer.parseInt(classId),
						showClasses);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "/modifyClasses";
	}
	//删除某个班级的信息
	@RequestMapping("/deleteClasses")
	public ModelAndView deleteClassesInfo( HttpServletRequest request,
			HttpServletResponse response) throws Exception {	

		String  classId = request.getParameter("classId");
		ClassAccountService cas = new ClassAccountService();
		cas.deleteClassInfo(classId);
		int current = 0;
		PageBean3  pageBean3 = cas.findByPage(current, 4);
		request.setAttribute("pageBean", pageBean3);
		return new ModelAndView("/classes", "pageBean", pageBean3);
	}
}
